using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace UNGDUNG
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        clsDatabases cls;
        HelpProvider hp;
        private void frmLogin_Load(object sender, EventArgs e)
        {

            AssignHelp();
            cls = new clsDatabases("");
            ShowDatabase();
            ShowLanguage();
            ShowRoles();
            ShowLogin(clsStatic.strInput);
            

        }
        void ShowDatabase()
        {
            DataTable dt = cls.GetTable("master.dbo.sysdatabases", "name", "", " order by name asc");
            if (dt != null)
            {                
                cbDatabases.DataSource = dt;
                cbDatabases.DisplayMember = "name";
                cbDatabases.ValueMember = "name";
            }  
        }
        void ShowRoles()
        {        
            DataTable dt = cls.GetTable("master.dbo.spt_values v1,"+
                "master.dbo.spt_values v2 ", "v2.name", "where v1.low = 0 and "+
				    "v1.type = 'SRV' and "+
				    "v2.low = -1 and v2.type = 'SRV' "+
                       "and v1.number = v2.number", " ");
            if (dt != null)
            {
                foreach (DataRow r in dt.Rows)
                {
                    checkedListBox1.Items.Add(r[0]);
                }
            }
        }
        void ShowLanguage()
        {
            DataTable dt = cls.GetTable("master.dbo.syslanguages", "name", "", " order by name asc");
            if (dt != null)
            {
                cbLanguages.DataSource = dt;
                cbLanguages.DisplayMember = "name";
                cbLanguages.ValueMember = "name";
            }
        }
        void ShowLogin(string name)
        {            
            object[] info = cls.ShowLogin(name);
            if (info != null)
            {
                label1.Text = info[0].ToString();
                label11.Text = "Created: " + info[2].ToString();
                if (info[1].ToString().Equals("0"))
                {
                    SQL.Enabled = true;
                    txtPWD.Text = "********";
                    NT.Enabled = false;
                    rdSQL.Checked = true;
                }
                else
                {
                    SQL.Enabled = false;
                    txtPWD.Text = "";
                    cbServers.Text = label1.Text.Substring(0,label1.Text.IndexOf(@"\"));
                    NT.Enabled = true;
                    rdNT.Checked = true;
                }
                cbDatabases.Text = info[3].ToString();
                cbLanguages.Text = info[4].ToString();
            }            
        }
    
        void  AssignHelp()
        {
            hp = new HelpProvider();
            hp.HelpNamespace = Application.StartupPath.Replace(@"\bin\Debug", "") + @"\help.htm";
            hp.SetHelpKeyword(this.button3, "Database");
            hp.SetHelpNavigator(this.button3, HelpNavigator.Index);
            hp.SetHelpString(this.button3, "Database");
            hp.SetShowHelp(this.button3, true);
        }
         
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}